## Script (Python) "getProjectTasks"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=project
##title=getProjectTasks
##

""" Get a dictionary with a projects tasks and info """

tasks = context.portal_catalog.searchResults(portal_type=('Task',), path={'query':'/'.join(project.getPhysicalPath()),'depth':1}, sort_on='Date', sort_order='reverse',)
tasksExtras = context.portal_catalog.searchResults(portal_type=('TaskExtra',), path={'query':'/'.join(project.getPhysicalPath()),'depth':1}, sort_on='Date', sort_order='reverse',)
taskfolders = context.portal_catalog.searchResults(portal_type=('Taskfolder',), path={'query':'/'.join(project.getPhysicalPath()),'depth':1}, sort_on='Date', sort_order='reverse',)

taskDict = {}
taskList = []
estimated = 0
used = 0
projectProgress = 0

if len(tasks) > 0:

  for task in tasks:
    taskList.append(task)
    if task.getEstimated_hours != None:
      estimated = estimated + task.getEstimated_hours
    if task.getUsed_hours != None:
        used = used + task.getUsed_hours

if len(tasksExtras) > 0:

  for taskExtra in tasksExtras:
    taskList.append(taskExtra)
    if taskExtra.getEstimated_hours != None:
      estimated = estimated + taskExtra.getEstimated_hours
    if taskExtra.getUsed_hours != None:
        used = used + taskExtra.getUsed_hours

if len(taskfolders) > 0:

  for folder in taskfolders:
    taskList.append(folder)
    object = folder.getObject()
    tmpDict = context.scripts.getAllProjectTasks(object)
    estimated = estimated + tmpDict['estimated']
    used = used + tmpDict['used']

if used != 0 and estimated != 0:
    projectProgress = (used*100)/estimated
else:
    projectProgress = 0

taskDict['tasks']=taskList
taskDict['estimated']=estimated
taskDict['used']=used
taskDict['progress']= projectProgress

return taskDict

